﻿#pragma once
#include "FlatForm.h"
#include "public.h"
#include "Utils.h"
#include "TRichTextBox.h"
#define CHARA_SELF		0
#define CHARA_TARG		1
#define FACE_COUNT		162
#define MODE_CTRL_ENTER 0
#define MODE_ENTER_ONLY 1
namespace rjksClient {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Threading;
	using namespace Newtonsoft::Json::Linq;

	/// <summary>
	/// frmIM 摘要
	/// </summary>
	public ref class frmIM : FlatForm
	{
	public:
		delegate void JArraryParamDelegate(JArray^);
		UserInfo targuinfo;
		UserInfo uinfo;
		Thread^ PushThread;
		int last_talk;
		int last_msg_time;
		float push_interval;
		int send_mode;
		Semaphore^ rtxtConversation_lock;
	private: System::Windows::Forms::Button^  btnFace;
	private: System::Windows::Forms::Panel^  panelFace;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Label^  lblBG2;
	private: System::Windows::Forms::Button^  btnCtrlEnterSel;
	private: System::Windows::Forms::ContextMenuStrip^  contextMenuStrip1;
	private: System::Windows::Forms::ToolStripMenuItem^  itemSetCtrlEnter;
	private: System::Windows::Forms::ToolStripMenuItem^  itemSetEnter;


	public: 

	public: 
		int accum_error;
	public:
		frmIM(void)
		{
			InitializeComponent();
			//
			//TODO: 在此处添加构造函数代码
			//
			MaxMinConfig=MAXMIN;
			setBoarderColor(0x3498db);
			rtxtConversation_lock=gcnew Semaphore(1,1);
		}
		void GetPush();
		void AddResponseWords(JArray^ ja);
		void Append2Conversation(String^ input);
		void Append2Conversation(String^ input, Color color);
		void Append2Conversation(String^ input, System::Drawing::Font^ font);
		void Append2Conversation(String^ input, Color color, System::Drawing::Font^ font);
		void Append2Conversation(String^ input, Color color, Color bkcolor,System::Drawing::Font^ font);
		void _AppendText2Conversation(String^ input, Color color, Color bkcolor,System::Drawing::Font^ font,bool newline);
	protected:
		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		~frmIM()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::SplitContainer^  splitContainer1;
	private: System::Windows::Forms::RichTextBox^  rtxtSend;

	private: System::Windows::Forms::Button^  btnClear;
	private: System::Windows::Forms::Button^  btnSend;
	private: rjksClient::TRichTextBox^  rtxtConversation;
	private: System::ComponentModel::IContainer^  components;

			 //private: Microsoft::VisualBasic::PowerPacks::LineShape^  lineShape1;

	protected: 

	private:
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要
		/// 使用代码编辑器修改此方法的内容。
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			this->splitContainer1 = (gcnew System::Windows::Forms::SplitContainer());
			this->panelFace = (gcnew System::Windows::Forms::Panel());
			this->rtxtConversation = (gcnew rjksClient::TRichTextBox());
			this->btnCtrlEnterSel = (gcnew System::Windows::Forms::Button());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->btnFace = (gcnew System::Windows::Forms::Button());
			this->rtxtSend = (gcnew System::Windows::Forms::RichTextBox());
			this->btnClear = (gcnew System::Windows::Forms::Button());
			this->btnSend = (gcnew System::Windows::Forms::Button());
			this->lblBG2 = (gcnew System::Windows::Forms::Label());
			this->contextMenuStrip1 = (gcnew System::Windows::Forms::ContextMenuStrip(this->components));
			this->itemSetCtrlEnter = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->itemSetEnter = (gcnew System::Windows::Forms::ToolStripMenuItem());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->splitContainer1))->BeginInit();
			this->splitContainer1->Panel1->SuspendLayout();
			this->splitContainer1->Panel2->SuspendLayout();
			this->splitContainer1->SuspendLayout();
			this->contextMenuStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// splitContainer1
			// 
			this->splitContainer1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
				| System::Windows::Forms::AnchorStyles::Left) 
				| System::Windows::Forms::AnchorStyles::Right));
			this->splitContainer1->BackColor = System::Drawing::Color::White;
			this->splitContainer1->Location = System::Drawing::Point(5, 28);
			this->splitContainer1->Name = L"splitContainer1";
			this->splitContainer1->Orientation = System::Windows::Forms::Orientation::Horizontal;
			// 
			// splitContainer1.Panel1
			// 
			this->splitContainer1->Panel1->Controls->Add(this->panelFace);
			this->splitContainer1->Panel1->Controls->Add(this->rtxtConversation);
			// 
			// splitContainer1.Panel2
			// 
			this->splitContainer1->Panel2->Controls->Add(this->btnCtrlEnterSel);
			this->splitContainer1->Panel2->Controls->Add(this->button1);
			this->splitContainer1->Panel2->Controls->Add(this->btnFace);
			this->splitContainer1->Panel2->Controls->Add(this->rtxtSend);
			this->splitContainer1->Panel2->Controls->Add(this->btnClear);
			this->splitContainer1->Panel2->Controls->Add(this->btnSend);
			this->splitContainer1->Panel2->Controls->Add(this->lblBG2);
			this->splitContainer1->Size = System::Drawing::Size(830, 560);
			this->splitContainer1->SplitterDistance = 348;
			this->splitContainer1->TabIndex = 5;
			// 
			// panelFace
			// 
			this->panelFace->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
			this->panelFace->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(236)), static_cast<System::Int32>(static_cast<System::Byte>(240)), 
				static_cast<System::Int32>(static_cast<System::Byte>(241)));
			this->panelFace->Location = System::Drawing::Point(7, 90);
			this->panelFace->Name = L"panelFace";
			this->panelFace->Size = System::Drawing::Size(810, 259);
			this->panelFace->TabIndex = 7;
			this->panelFace->Visible = false;
			// 
			// rtxtConversation
			// 
			this->rtxtConversation->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
				| System::Windows::Forms::AnchorStyles::Left) 
				| System::Windows::Forms::AnchorStyles::Right));
			this->rtxtConversation->AutoWordSelection = true;
			this->rtxtConversation->BackColor = System::Drawing::Color::White;
			this->rtxtConversation->BorderStyle = System::Windows::Forms::BorderStyle::None;
			this->rtxtConversation->Font = (gcnew System::Drawing::Font(L"微软雅黑", 10.2F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->rtxtConversation->Location = System::Drawing::Point(6, 4);
			this->rtxtConversation->Margin = System::Windows::Forms::Padding(3, 4, 3, 4);
			this->rtxtConversation->Name = L"rtxtConversation";
			this->rtxtConversation->ReadOnly = true;
			this->rtxtConversation->ScrollBars = System::Windows::Forms::RichTextBoxScrollBars::Vertical;
			this->rtxtConversation->Size = System::Drawing::Size(815, 345);
			this->rtxtConversation->TabIndex = 6;
			this->rtxtConversation->Text = L"";
			// 
			// btnCtrlEnterSel
			// 
			this->btnCtrlEnterSel->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->btnCtrlEnterSel->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(52)), static_cast<System::Int32>(static_cast<System::Byte>(152)), 
				static_cast<System::Int32>(static_cast<System::Byte>(219)));
			this->btnCtrlEnterSel->FlatAppearance->BorderColor = System::Drawing::Color::White;
			this->btnCtrlEnterSel->FlatAppearance->BorderSize = 2;
			this->btnCtrlEnterSel->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->btnCtrlEnterSel->ForeColor = System::Drawing::Color::White;
			this->btnCtrlEnterSel->Location = System::Drawing::Point(806, 164);
			this->btnCtrlEnterSel->Name = L"btnCtrlEnterSel";
			this->btnCtrlEnterSel->Size = System::Drawing::Size(20, 32);
			this->btnCtrlEnterSel->TabIndex = 14;
			this->btnCtrlEnterSel->Text = L"▼";
			this->btnCtrlEnterSel->UseVisualStyleBackColor = false;
			this->btnCtrlEnterSel->Click += gcnew System::EventHandler(this, &frmIM::btnCtrlEnterSel_Click);
			// 
			// button1
			// 
			this->button1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
			this->button1->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(52)), static_cast<System::Int32>(static_cast<System::Byte>(152)), 
				static_cast<System::Int32>(static_cast<System::Byte>(219)));
			this->button1->FlatAppearance->BorderColor = System::Drawing::Color::White;
			this->button1->FlatAppearance->BorderSize = 2;
			this->button1->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->button1->Font = (gcnew System::Drawing::Font(L"楷体", 15, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->button1->ForeColor = System::Drawing::Color::White;
			this->button1->Location = System::Drawing::Point(67, 164);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(45, 37);
			this->button1->TabIndex = 12;
			this->button1->Text = L"←";
			this->button1->UseVisualStyleBackColor = false;
			this->button1->Click += gcnew System::EventHandler(this, &frmIM::button1_Click);
			// 
			// btnFace
			// 
			this->btnFace->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
			this->btnFace->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(52)), static_cast<System::Int32>(static_cast<System::Byte>(152)), 
				static_cast<System::Int32>(static_cast<System::Byte>(219)));
			this->btnFace->FlatAppearance->BorderColor = System::Drawing::Color::White;
			this->btnFace->FlatAppearance->BorderSize = 2;
			this->btnFace->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->btnFace->Font = (gcnew System::Drawing::Font(L"宋体", 15, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->btnFace->ForeColor = System::Drawing::Color::White;
			this->btnFace->Location = System::Drawing::Point(7, 164);
			this->btnFace->Name = L"btnFace";
			this->btnFace->Size = System::Drawing::Size(45, 37);
			this->btnFace->TabIndex = 11;
			this->btnFace->Text = L"☺";
			this->btnFace->UseVisualStyleBackColor = false;
			this->btnFace->Click += gcnew System::EventHandler(this, &frmIM::btnFace_Click);
			// 
			// rtxtSend
			// 
			this->rtxtSend->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
				| System::Windows::Forms::AnchorStyles::Left) 
				| System::Windows::Forms::AnchorStyles::Right));
			this->rtxtSend->AutoWordSelection = true;
			this->rtxtSend->BackColor = System::Drawing::Color::WhiteSmoke;
			this->rtxtSend->BorderStyle = System::Windows::Forms::BorderStyle::None;
			this->rtxtSend->Font = (gcnew System::Drawing::Font(L"微软雅黑", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->rtxtSend->Location = System::Drawing::Point(1, 4);
			this->rtxtSend->Margin = System::Windows::Forms::Padding(3, 4, 3, 4);
			this->rtxtSend->Name = L"rtxtSend";
			this->rtxtSend->ScrollBars = System::Windows::Forms::RichTextBoxScrollBars::Vertical;
			this->rtxtSend->Size = System::Drawing::Size(830, 153);
			this->rtxtSend->TabIndex = 6;
			this->rtxtSend->Text = L"";
			this->rtxtSend->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &frmIM::rtxtSend_Down);
			// 
			// btnClear
			// 
			this->btnClear->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->btnClear->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(231)), static_cast<System::Int32>(static_cast<System::Byte>(76)), 
				static_cast<System::Int32>(static_cast<System::Byte>(60)));
			this->btnClear->FlatAppearance->BorderColor = System::Drawing::Color::White;
			this->btnClear->FlatAppearance->BorderSize = 2;
			this->btnClear->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->btnClear->ForeColor = System::Drawing::Color::White;
			this->btnClear->Location = System::Drawing::Point(620, 164);
			this->btnClear->Name = L"btnClear";
			this->btnClear->Size = System::Drawing::Size(99, 32);
			this->btnClear->TabIndex = 9;
			this->btnClear->Text = L"清除";
			this->btnClear->UseVisualStyleBackColor = false;
			this->btnClear->Click += gcnew System::EventHandler(this, &frmIM::btnClear_Click);
			// 
			// btnSend
			// 
			this->btnSend->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->btnSend->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(52)), static_cast<System::Int32>(static_cast<System::Byte>(152)), 
				static_cast<System::Int32>(static_cast<System::Byte>(219)));
			this->btnSend->FlatAppearance->BorderColor = System::Drawing::Color::White;
			this->btnSend->FlatAppearance->BorderSize = 2;
			this->btnSend->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->btnSend->ForeColor = System::Drawing::Color::White;
			this->btnSend->Location = System::Drawing::Point(729, 164);
			this->btnSend->Name = L"btnSend";
			this->btnSend->Size = System::Drawing::Size(79, 32);
			this->btnSend->TabIndex = 8;
			this->btnSend->Text = L"发送";
			this->btnSend->UseVisualStyleBackColor = false;
			this->btnSend->Click += gcnew System::EventHandler(this, &frmIM::btnSend_Click);
			// 
			// lblBG2
			// 
			this->lblBG2->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(52)), static_cast<System::Int32>(static_cast<System::Byte>(152)), 
				static_cast<System::Int32>(static_cast<System::Byte>(219)));
			this->lblBG2->Dock = System::Windows::Forms::DockStyle::Fill;
			this->lblBG2->Location = System::Drawing::Point(0, 0);
			this->lblBG2->Name = L"lblBG2";
			this->lblBG2->Size = System::Drawing::Size(830, 208);
			this->lblBG2->TabIndex = 13;
			// 
			// contextMenuStrip1
			// 
			this->contextMenuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->itemSetCtrlEnter, 
				this->itemSetEnter});
			this->contextMenuStrip1->Name = L"contextMenuStrip1";
			this->contextMenuStrip1->Size = System::Drawing::Size(188, 74);
			// 
			// itemSetCtrlEnter
			// 
			this->itemSetCtrlEnter->Checked = true;
			this->itemSetCtrlEnter->CheckState = System::Windows::Forms::CheckState::Checked;
			this->itemSetCtrlEnter->Name = L"itemSetCtrlEnter";
			this->itemSetCtrlEnter->Size = System::Drawing::Size(187, 24);
			this->itemSetCtrlEnter->Text = L"Ctrl+Enter 发送";
			this->itemSetCtrlEnter->Click += gcnew System::EventHandler(this, &frmIM::itemSetCtrlEnter_Click);
			// 
			// itemSetEnter
			// 
			this->itemSetEnter->Name = L"itemSetEnter";
			this->itemSetEnter->Size = System::Drawing::Size(187, 24);
			this->itemSetEnter->Text = L"Enter 发送";
			this->itemSetEnter->Click += gcnew System::EventHandler(this, &frmIM::itemSetEnter_Click);
			// 
			// frmIM
			// 
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Inherit;
			this->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(52)), static_cast<System::Int32>(static_cast<System::Byte>(152)), 
				static_cast<System::Int32>(static_cast<System::Byte>(219)));
			this->ClientSize = System::Drawing::Size(840, 593);
			this->Controls->Add(this->splitContainer1);
			this->Name = L"frmIM";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &frmIM::frmIM_FormClosing);
			this->Load += gcnew System::EventHandler(this, &frmIM::frmIM_Load);
			this->Controls->SetChildIndex(this->splitContainer1, 0);
			this->splitContainer1->Panel1->ResumeLayout(false);
			this->splitContainer1->Panel2->ResumeLayout(false);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->splitContainer1))->EndInit();
			this->splitContainer1->ResumeLayout(false);
			this->contextMenuStrip1->ResumeLayout(false);
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void btnClear_Click(System::Object^  sender, System::EventArgs^  e) {
				 this->rtxtSend->Clear();
			 }
	private: System::Void btnSend_Click(System::Object^  sender, System::EventArgs^  e) ;
	private: System::Void frmIM_Load(System::Object^  sender, System::EventArgs^  e) {
				this->setTitleText("与 "+this->targuinfo.uname+" 聊天中");
				PushThread = gcnew Thread(gcnew ThreadStart(this,&frmIM::GetPush));
				PushThread->Start();
		 }
private: System::Void rtxtSend_Down(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
			 if (e->Modifiers == Keys::Control )
			{
				if(e->KeyCode == Keys::Enter){
					if(send_mode==MODE_CTRL_ENTER)
						btnSend_Click(sender,gcnew System::EventArgs);
					else
						rtxtSend->AppendText("\n");
					e->Handled=true;
				}
				if(e->KeyCode == Keys::A)
					((RichTextBox^)sender)->SelectAll();
			}else{
				if(e->KeyCode == Keys::Enter){
					if(send_mode==MODE_ENTER_ONLY)
						btnSend_Click(sender,gcnew System::EventArgs);
					else
						rtxtSend->AppendText("\n");
					e->Handled=true;
				}
			 }
		 }
private: System::Void frmIM_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
			 this->PushThread->Abort();
		 }
private: System::Void btnFace_Click(System::Object^  sender, System::EventArgs^  e) {
			 panelFace->Visible=!panelFace->Visible;
			 btnFace->Text=panelFace->Visible?L"⊙":L"☺";
			 if(panelFace->Controls->Count==0)
				 loadFaces();
		 }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 if(panelFace->Visible&&rtxtSend->Text->Length>=6)
				 rtxtSend->Text=rtxtSend->Text->Substring(0,rtxtSend->Text->Length-5);
		 }
private: void loadFaces();
private: System::Void faceClickHdl(System::Object^  sender, System::EventArgs^  e) ;
private: System::Void btnCtrlEnterSel_Click(System::Object^  sender, System::EventArgs^  e) {
			 contextMenuStrip1->Show(this->Right-190,this->Bottom+10);
		 }
private: System::Void itemSetCtrlEnter_Click(System::Object^  sender, System::EventArgs^  e) {
			 itemSetCtrlEnter->Checked=!itemSetCtrlEnter->Checked;
			 itemSetEnter->Checked=!itemSetEnter->Checked;
			 send_mode=MODE_CTRL_ENTER;
		 }
private: System::Void itemSetEnter_Click(System::Object^  sender, System::EventArgs^  e) {
			 itemSetCtrlEnter->Checked=!itemSetCtrlEnter->Checked;
			 itemSetEnter->Checked=!itemSetEnter->Checked;
			 send_mode=MODE_ENTER_ONLY;
		 }
};
}
